
import javax
import java

from org.memesis.pandorasbook.ui import PandoraTextPane

class ThinkNet(object):
    
    class SendText(java.awt.event.ActionListener):
        def __init__(self, app):
            self.app = app
            
        def actionPerformed(self, act):
            channel = self.app.thinkNetPanes.getTitleAt(self.app.thinkNetPanes.getSelectedIndex())
            strr = "th %s=%s" % (channel, self.app.input.getText())
            mq.publish(applet.getProperty("jms-channel"), strr)
            self.app.input.setText("")
            
    class GetText(javax.jms.MessageListener):
        def __init__(self, app):
            self.app = app
            
        def onMessage(self, m):
            t = m.text
            channel, text = t.split(';;',1)
            ind = self.app.thinkNetPanes.indexOfTab(channel)
            if ind == -1:
                self.app.outputs[channel] = PandoraTextPane()
                self.app.outputs[channel].setEditable(False)
                scrolly = javax.swing.JScrollPane(self.app.outputs[channel])
                scrolly.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS)
                self.app.thinkNetPanes.addTab(channel,scrolly)
            doc = self.app.outputs[channel].getStyledDocument()
            doc.insertString(doc.getLength(),text,None)
            doc.insertString(doc.getLength(),"\n",None)
            if self.app.input.getText() == "":
                self.app.thinkNetPanes.setSelectedIndex(self.app.thinkNetPanes.indexOfTab(channel))
    
    def initGui(self, frame):
        self.frame = frame
        frame.setTitle("ThinkNet")
        
        self.thinkNetPanes = javax.swing.JTabbedPane()
        self.thinkNetPanes.setTabPlacement(javax.swing.JTabbedPane.BOTTOM)
        
        self.input = javax.swing.JTextField()
        self.inputListener = ThinkNet.SendText(self)
        self.input.addActionListener(self.inputListener)
        
        self.frame.getContentPane().add(self.thinkNetPanes, java.awt.BorderLayout.CENTER)
        self.frame.getContentPane().add(self.input, java.awt.BorderLayout.PAGE_END)
        
        self.frame.pack()
        self.frame.setSize(320, 400)
       
    
    def __init__(self, frame):
        
        self.initGui(frame)
                
        self.outputs = {}
        
        self.messageListener = ThinkNet.GetText(self)
        mq.subscribe(applet.getProperty("jms-recv-channel"), self.messageListener)




c = ThinkNet(frame)